﻿using BusinessPlatform.Core.Criteria;
using BusinessPlatform.Core.EntityParser;
using NewStartHanldingUI.Menus;
using NewStartModels.Models.Authorities;
using NewStartModels.Models.Menus;
using NewStartModels.Models.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NewStartHanldingUI.Login
{
    public class Login : ILogin
    {
        #region Constructor
        public Login()
        {
            this.LoginEvent += new BusinessSystemBaseEventHanlder(Login_LoginEvent);
        }
        #endregion
        #region Properties
        /// <summary>
        /// 账号
        /// </summary>
        public string UserCode { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 账套
        /// </summary>
        public string Company { get; set; }
        /// <summary>
        /// 语言
        /// </summary>
        public string Language { get; set; }
        /// <summary>
        /// 机器名
        /// </summary>
        public string LoginMachineName { get; set; }
        /// <summary>
        /// IP地址
        /// </summary>
        public string LoginMachineIP { get; set; }
        #endregion
        #region events
        public event BusinessSystemBaseEventHanlder LoginEvent;
        void Login_LoginEvent(object sender, LoginEventHandlerArgs e)
        {
            if (e.Status)
            {
                EntityParser iParser;
                ICriteria cri;
                ICondition con;
                IList<IAuthoritier> Authors = null;
                #region 加载权限
                iParser = new EntityParser();
                cri = new Criteria();
                con = cri.Conditions.New();
                con.FieldName = "UserID";
                con.Operation = EnumOperateSymbol.EQUAL;
                con.ConditionValue = CurrentUser.CurrentUser.Instance.UserId.ToString();
                var result = iParser.Search<IAuthoritier>(cri);
                if (result.ResultFlag)
                {
                    Authors = result.BusinessObject.ToList<IAuthoritier>();
                    CurrentUser.CurrentUser.Instance.Authors = Authors;
                }
                #endregion
                #region 加载菜单
                IMenus menus = new Menus.Menus();
                foreach (IAuthoritier Author in Authors)
                {
                    iParser = new EntityParser();
                    cri = new Criteria();
                    con = cri.Conditions.New();
                    con.FieldName = "MenuUID";
                    con.Operation = EnumOperateSymbol.EQUAL;
                    con.ConditionValue = Author.UniqueID.ToString();
                    var ar = iParser.Search<NewStartModels.Models.Menus.IMenu>(cri);
                    if (ar.ResultFlag)
                    {
                        NewStartModels.Models.Menus.IMenu menu = ar.BusinessObject.FirstOrDefault<NewStartModels.Models.Menus.IMenu>();
                        menus.Add(menu);
                    }
                }
                CurrentUser.CurrentUser.Instance.Menus = menus;
                #endregion
            }
        }
        protected virtual string OnLogin()
        {
            if (LoginEvent != null)
            {
                LoginEventHandlerArgs LoginArgs = new LoginEventHandlerArgs();
                #region 验证登陆信息
                EntityParser iParser = new EntityParser();
                ICriteria cri = new Criteria();
                ICondition con = cri.Conditions.New();
                con.FieldName = "UserCode";
                con.Operation = EnumOperateSymbol.EQUAL;
                con.ConditionValue = UserCode;
                con = cri.Conditions.New();
                con.Relationship = EnumRelationship.AND;
                con.FieldName = "Password";
                con.Operation = EnumOperateSymbol.EQUAL;
                con.ConditionValue = Password;
                var result = iParser.Search<IUser>(cri);
                if (result.ResultFlag)
                {
                    #region 设置当前用户信息
                    var iUser = result.BusinessObject.FirstOrDefault<IUser>();
                    CurrentUser.CurrentUser curUser = CurrentUser.CurrentUser.Instance as CurrentUser.CurrentUser;
                    curUser.UserId = iUser.UserID;
                    curUser.UserCode = iUser.UserCode;
                    curUser.UserName = iUser.UserName;
                    curUser.Password = iUser.Password;
                    curUser.IsSupperUser = iUser.IsSupperUser;
                    curUser.LoginDateTime = DateTime.Now;
                    curUser.LoginMachineIP = this.LoginMachineIP;
                    curUser.LoginMachineName = this.LoginMachineName;
                    curUser.LoginLanguage = this.Language;
                    #endregion
                    LoginArgs.Status = true;
                    LoginEvent(this, LoginArgs);
                    return "";
                }
                else
                {
                    return "用户名或密码不正确，请检查！";
                }
                #endregion
            }
            return "";
        }
        public string myLogin()
        {
            return OnLogin();
        }
        #endregion
    }
}
